package com.ibiaoke.system.dao;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.system.entity.Advert;
import com.ibiaoke.system.entity.AdvertExample;
import com.ibiaoke.system.mapper.AdvertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 */
@Repository
public class AdvertDao {
    @Autowired
    private AdvertMapper advertMapper;

    /**
     * 添加Advert
     * @param advert
     * @return
     */
    public int saveBanner(Advert advert){
        return advertMapper.insert(advert);
    }

    /**
     * 删除Advert
     * @param advertId
     * @return
     */
    public int deleteBannerById(Integer advertId){
        Advert advert = new Advert();
        advert.setId(advertId);
        advert.setDelStatus(1);
        int result = advertMapper.updateByPrimaryKeySelective(advert);
        return result;
    }

    /**
     * 修改Advert
     * @param advert
     * @return
     */
    public int updateBannerById(Advert advert){
        AdvertExample example = new AdvertExample();
        example.createCriteria().andIdEqualTo(advert.getId());
        int result = advertMapper.updateByExampleSelective(advert, example);
        return result;
    }

    /**
     * 查询Advert
     * @param position
     * @param showFlag
     *@param page  @return
     */
    public List<Advert> listBanner(Integer position, Integer showFlag, Page page){
        AdvertExample example = new AdvertExample();
        AdvertExample.Criteria criteria = example.createCriteria().andPositionEqualTo(position).andDelStatusEqualTo(CommonConsts.NO);
        if(showFlag!=null) {
            criteria.andShowFlagEqualTo(showFlag);
        }
        page.setTotalCount(advertMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("sort asc");
        return  advertMapper.selectByExample(example);
    }
}